package com.wutianyi.jackson;

import java.io.File;
import java.io.IOException;

import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;

import com.wutianyi.jackson.User.Gender;

public class StreamingWriter
{
    public static void main(String[] args) throws IOException
    {
        JsonFactory f = new JsonFactory();
        JsonGenerator g = f.createJsonGenerator(new File("user-streaming-write.json"), JsonEncoding.UTF8);

        g.writeStartObject();
        g.writeObjectFieldStart("name");
        g.writeStringField("first", "Joe");
        g.writeStringField("last", "Sixpack");
        g.writeNumberField("num", 1.0);
        g.writeFieldName("chars");
        g.writeString(new char[]{'a','b','c'}, 0, 3);
        g.writeEndObject(); // for field 'name'
        g.writeStringField("gender", Gender.MALE.toString());
        g.writeBooleanField("verified", false);
        g.writeFieldName("userImage"); // no 'writeBinaryField' (yet?)
        byte[] binaryData = "".getBytes();
        g.writeBinary(binaryData);
        g.writeEndObject();
        g.close(); // important: will force flushing of output, close underlying
                   // output stream
    }
}
